package com.ruoyi.project.company.controller;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.List;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.ResourceUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.framework.web.page.TableDataInfo;
import com.ruoyi.project.company.domain.Company;
import com.ruoyi.project.company.service.ICompanyService;
import com.ruoyi.project.system.dict.domain.DictData;

/**
 * 
 * @作者 290903152@qq.com
 * @时间 202003
 * @功能 毕业设计
 *
 */
@Controller
@RequestMapping("/company/relation")
public class CompanyController extends BaseController{
	private String prefix = "company/relation";

	@Autowired
	private ICompanyService companyService;
	
    @GetMapping()
    public String config()
    {
        return prefix + "/company";
    }
	
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(Company cp)
    {
        startPage();
        List<Company> list = companyService.selectCompanyList(cp);
        return getDataTable(list);
    }
    

    @GetMapping("/add")
    public String add(ModelMap mmap)
    {
        return prefix + "/add";
    }

    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(@Validated Company cp)
    {
        return toAjax(companyService.insertCompany(cp));
    }

    /**
     * 修改字典类型
     */
    @GetMapping("/edit/{id}")
    public String edit(@PathVariable("id") String id, ModelMap mmap)
    {
        mmap.put("commany", companyService.selectCompanyById(id));
        return prefix + "/edit";
    }

    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(@Validated Company cp)
    {
        return toAjax(companyService.updateCompany(cp));
    }
    
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(companyService.deleteCompany(ids));
    }
    
    @GetMapping("/goUpload")
    public String goUpload(ModelMap mmap)
    {
        return prefix + "/upload";
    }
    
    @PostMapping("/upload")
    @ResponseBody
	public AjaxResult upload(@RequestParam("file") MultipartFile file) throws IOException {
		String filePath = file.getOriginalFilename();
		File path = new File(ResourceUtils.getURL("classpath:").getPath());
		if(!path.exists()) path = new File("");
		File upload = new File(path.getAbsolutePath(),"static/upload/");
		if(!upload.exists()) upload.mkdirs();
		
		BufferedOutputStream outputStream = new BufferedOutputStream(new FileOutputStream(upload+"/"+filePath));
		outputStream.write(file.getBytes());
		outputStream.flush();
		outputStream.close();
		companyService.ExcelData(file);
		return success();
	}
}
